//
//  XZ_QmuiController.m
//  Project_Demo
//
//  Created by caoxiaozhu on 2018/7/25.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

#import "XZ_QmuiController.h"

@interface XZ_QmuiController ()<QMUIAlbumViewControllerDelegate,QMUIImagePickerViewControllerDelegate,QDSingleImagePickerPreviewViewControllerDelegate>

@property(nonatomic,strong)UIImageView *selectedImageView;

@end

@implementation XZ_QmuiController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [QMUIButton createButtonWithFrame:CGRectMake(100, 100, 200, 30) buttonType:kButtonRed title:@"点击我" tag:1 target:self action:@selector(clickMe)];
    btn.center = self.view.center;
    
    _selectedImageView = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 300, 300)];
    _selectedImageView.centerX = self.view.centerX;
    [self.view addSubview:_selectedImageView];
    
    
}

-(void)clickMe{
    
//    //alertView
//    QMUIAlertController *alert = [QMUIAlertController alertControllerWithTitle:@"你好" message:@"没事" preferredStyle:QMUIAlertControllerStyleAlert];
//
//    QMUIAlertAction *action = [QMUIAlertAction actionWithTitle:@"haha" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
//    }];
//
//
//    [alert addAction:action];
//    [alert showWithAnimated:true];
    
//    QMUIAssetsManager *assets = [QMUIAssetsManager sharedInstance];
//    [QMUIAssetsManager requestAuthorization:nil];
//    [assets enumerateAllAlbumsWithAlbumContentType:QMUIAlbumContentTypeAll usingBlock:^(QMUIAssetsGroup *resultAssetsGroup) {
//        NSLog(@"%@",resultAssetsGroup);
//    }];
    
    [QMUIAssetsManager requestAuthorization:nil];
    
    if ([QMUIAssetsManager authorizationStatus] == QMUIAssetAuthorizationStatusAuthorized) {
        NSLog(@"已经授权");
        
        
        [self presentAlbumViewControllerWithTitle:@"haha"];
    }
    
//    QMUIImagePickerViewController *imageViewPicker = [QMUIImagePickerViewController appearance];
//    [self presentViewController:imageViewPicker animated:true completion:nil];
}

- (void)presentAlbumViewControllerWithTitle:(NSString *)title {
    
    // 创建一个 QMUIAlbumViewController 实例用于呈现相簿列表
    QMUIAlbumViewController *albumViewController = [[QMUIAlbumViewController alloc] init];
    albumViewController.albumViewControllerDelegate = self;
    albumViewController.contentType = QMUIAlbumContentTypeAll;
    albumViewController.title = title;
//    QMUINavigationController *navigationController = [[QMUINavigationController alloc] initWithRootViewController:albumViewController];
    XZ_BaseNavigationController *navi = [[XZ_BaseNavigationController alloc]initWithRootViewController:albumViewController];
    
    // 获取最近发送图片时使用过的相簿，如果有则直接进入该相簿
    [albumViewController pickLastAlbumGroupDirectlyIfCan];
    
    [self presentViewController:navi animated:YES completion:NULL];
}

-(QMUIImagePickerViewController *)imagePickerViewControllerForAlbumViewController:(QMUIAlbumViewController *)albumViewController{
    QMUIImagePickerViewController *imagePickerViewController = [[QMUIImagePickerViewController alloc] init];
    imagePickerViewController.allowsMultipleSelection = false;
    imagePickerViewController.imagePickerViewControllerDelegate = self;
    return imagePickerViewController;
}


-(QMUIImagePickerPreviewViewController *)imagePickerPreviewViewControllerForImagePickerViewController:(QMUIImagePickerViewController *)imagePickerViewController{

    QDSingleImagePickerPreviewViewController *imagePickerPreviewViewController = [[QDSingleImagePickerPreviewViewController alloc] init];
    imagePickerPreviewViewController.delegate = self;
    imagePickerPreviewViewController.view.tag = imagePickerViewController.view.tag;
    return imagePickerPreviewViewController;
}

#pragma mark - <QDSingleImagePickerPreviewViewControllerDelegate>

- (void)imagePickerPreviewViewController:(QDSingleImagePickerPreviewViewController *)imagePickerPreviewViewController didSelectImageWithImagesAsset:(QMUIAsset *)imageAsset {
    // 储存最近选择了图片的相册，方便下次直接进入该相册
    [QMUIImagePickerHelper updateLastestAlbumWithAssetsGroup:imagePickerPreviewViewController.assetsGroup ablumContentType:QMUIAlbumContentTypeAll userIdentify:nil];
    // 显示 loading
    [self startLoading];
    [imageAsset requestImageData:^(NSData *imageData, NSDictionary<NSString *,id> *info, BOOL isGif, BOOL isHEIC) {
        UIImage *targetImage = [UIImage imageWithData:imageData];
        if (isHEIC) {
            // iOS 11 中新增 HEIF/HEVC 格式的资源，直接发送新格式的照片到不支持新格式的设备，照片可能会无法识别，可以先转换为通用的 JPEG 格式再进行使用。
            // 详细请浏览：https://github.com/QMUI/QMUI_iOS/issues/224
            targetImage = [UIImage imageWithData:UIImageJPEGRepresentation(targetImage, 1)];
        }
        [self performSelector:@selector(setAvatarWithAvatarImage:) withObject:targetImage afterDelay:1.8];
    }];
}


- (void)setAvatarWithAvatarImage:(UIImage *)avatarImage {
    [self stopLoading];

    _selectedImageView.image  = avatarImage;
}

#pragma mark - 业务方法

- (void)startLoading {
    [QMUITips showLoadingInView:self.view];
}

- (void)stopLoading {
    [QMUITips hideAllToastInView:self.view animated:YES];
}

@end
